package com.freewater.springboot.demo.aop.api.advices.introduction;

import org.springframework.aop.framework.ProxyFactory;

public class Main {
    public static void main(String[] args) {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(new SomeBusiness());
        // As usual with introductions, the advisor must be per-instance, as it is stateful.
        // We need a different instance of LockMixinAdvisor, and hence LockMixin,
        // for each advised object. The advisor comprises part of the advised object’s state.
        // 每一个被Introduction的对象都应该有一个独立的对应的LockMixinAdvisor实例
        proxyFactory.addAdvisor(new LockMixinAdvisor()); // 等同于 proxyFactory.addAdvice(new LockMixin());
        proxyFactory.setProxyTargetClass(true);
        SomeBusiness someBusiness = (SomeBusiness) proxyFactory.getProxy();
        Lockable lockable = (Lockable) someBusiness;
        someBusiness.setData("freewater");
        System.out.println("new data: " + someBusiness.getData());
        try {
            lockable.lock();
            someBusiness.setData("freewater");
            System.out.println(someBusiness.getData());
        } catch (LockedException e) {
            System.out.println("error: " + e.getMessage());
        }
        lockable.unlock();
        someBusiness.setData("yujishui");
        System.out.println("new data: " + someBusiness.getData());
    }
}
